package com.qyer.util.log.util;

import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.binary.Base64;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Base64Util {
	// 加密
	public static String getBase64(String str) {
		byte[] b = null;
		String s = null;
		try {
			b = str.getBytes("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		if (b != null) {
			s = new BASE64Encoder().encode(b);
		}
		return s;
	}

	// 解密
	public static String getFromBase64(String s) {
		byte[] b = null;
		String result = null;
		if (s != null) {
			BASE64Decoder decoder = new BASE64Decoder();
			try {
				b = decoder.decodeBuffer(s);
				result = new String(b ,"GBK");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	/**
     * @param bytes
     * @return
     */
    public static String decode(final byte[] bytes) {
    	try {
			  return new String (Base64.decodeBase64(bytes));
		} catch (Exception e) {
			e.printStackTrace();
			return null ;
		}
    }

    /**
     * 二进制数据编码为BASE64字符串
     *
     * @param bytes
     * @return
     * @throws Exception
     */
    public static String encode(final byte[] bytes) {
        return new String(Base64.encodeBase64(bytes));
    }

	
	public static void main(String[] args) {
		String v="V2AJZFFvBzBTbVI3Cm4===";
		
		String res =decode(v.getBytes());
		
		System.out.println(res);
		
		System.out.println("YYYFSDLF".toLowerCase());
	}
}